<template>
  <div class="table-box">
    <pro-table ref="proTableRef" title="菜单列表" row-key="id" :columns="columns" :request-api="getTemplatePageApi">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button v-auth="'sys:template:add'" type="primary" :icon="CirclePlus" @click="handleAdd"> 新增模版 </el-button>
      </template>
      <!-- 模版编码 -->
      <template #code="{ row }">
        <el-tooltip effect="dark" content="点击复制" placement="top">
          <div v-copy="row.code" class="table-copy-box">
            <span>
              {{ row.code }}
            </span>
            <el-icon>
              <DocumentCopy />
            </el-icon>
          </div>
        </el-tooltip>
      </template>
      <!-- 状态 -->
      <template #isEnable="scope">
        <el-switch
          v-model="scope.row.isEnable"
          :loading="scope.row.loading"
          active-text="已启用"
          inactive-text="已禁用"
          inline-prompt
          :before-change="handleEnable.bind(scope, scope.row)"
        />
      </template>
      <!-- 菜单操作 -->
      <template #operation="{ row }">
        <el-button v-auth="'sys:template:edit'" type="primary" link :icon="EditPen" @click="handleEdit(row)"> 编辑 </el-button>
        <el-button v-auth="'sys:template:del'" type="danger" link :icon="Delete" @click="handleDelete(row)"> 删除 </el-button>
      </template>
    </pro-table>
    <!-- 编辑模版 -->
    <template-edit ref="editDialogRef" />
  </div>
</template>

<script setup lang="ts" name="templateManage">
import type { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import type { SysTemplate } from "@/api/interface";
import { ref } from "vue";
import { useHandleData } from "@/hooks/useHandleData";
import { statusConst } from "@/utils/dict";
import {
  addTemplateApi,
  editTemplateApi,
  switchEnableTemplateApi,
  deleteTemplateApi,
  getTemplatePageApi
} from "@/api/modules/system/template";
import { Delete, EditPen, CirclePlus, DocumentCopy } from "@element-plus/icons-vue";
import ProTable from "@/components/ProTable/index.vue";
import TemplateEdit from "./components/edit.vue";

/** 表格实例 */
const proTableRef = ref<ProTableInstance>();
/** 编辑弹窗实例 */
const editDialogRef = ref<InstanceType<typeof TemplateEdit> | null>(null);

/** 表格配置项 */
const columns: ColumnProps[] = [
  { prop: "name", label: "模版名称", align: "left", width: 200, search: { el: "input" } },
  { prop: "code", label: "模版编码", align: "left", width: 200, search: { el: "input" } },
  { prop: "fileName", label: "附件名称", align: "left" },
  { prop: "fileSize", label: "附件大小(K)", align: "left" },
  { prop: "filePath", label: "附件路径", align: "left" },
  { prop: "isEnable", label: "状态", width: 100, enum: statusConst, search: { el: "select" } },
  { prop: "operation", label: "操作", width: 180, fixed: "right" }
];

/**
 * 添加菜单
 *
 * @returns void
 */
const handleAdd = () => {
  const editParams = {
    title: "添加模版",
    template: {},
    getTableList: proTableRef.value?.getTableList,
    submitApi: addTemplateApi
  };
  editDialogRef.value?.acceptParams(editParams);
};

/**
 * 编辑模版
 *
 * @param param 选中的模版
 * @returns void
 */
const handleEdit = (param: Partial<SysTemplate.Result>) => {
  const editParams = {
    title: "编辑模版",
    template: { ...param },
    getTableList: proTableRef.value?.getTableList,
    submitApi: editTemplateApi
  };
  editDialogRef.value?.acceptParams(editParams);
};

/**
 * 启用/禁用模版
 *
 * @param param 选中的模版
 * @returns void
 */
const handleEnable = (param: Partial<SysTemplate.Result> & { loading?: boolean }) => {
  param.loading = true;
  return switchEnableTemplateApi({ id: param.id! })
    .then(({ data }) => {
      if (data) {
        return Promise.resolve(true);
      } else return Promise.reject(new Error("修改状态失败"));
    })
    .finally(() => {
      param.loading = false;
    });
};

/**
 * 删除模版
 *
 * @param param 选中的模版
 * @returns void
 */
const handleDelete = (param: Partial<SysTemplate.Result>) => {
  useHandleData(deleteTemplateApi, { ids: [param.id] }, `删除【${param.name}】模版`).then(res => {
    if (res) proTableRef.value?.getTableList();
  });
};
</script>
